<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActionDispatch::Assertions::SelectorAssertions</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/github.css" type="text/css" media="screen" />
<script src="../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Module</span> 
            ActionDispatch::Assertions::SelectorAssertions 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../../files/actionpack/lib/action_dispatch/testing/assertions/selector_rb.html">actionpack/lib/action_dispatch/testing/assertions/selector.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<p>Adds the <code>assert_select</code> method for use in <a
href="../../Rails.html">Rails</a> functional test cases, which can be used
to make assertions on the response <a href="../../HTML.html">HTML</a> of a
controller action. You can also call <code>assert_select</code> within
another <code>assert_select</code> to make assertions on elements selected
by the enclosing assertion.</p>

<p>Use <code>css_select</code> to select elements without making an
assertions, either from the response <a href="../../HTML.html">HTML</a> or
elements selected by the enclosing assertion.</p>

<p>In addition to <a href="../../HTML.html">HTML</a> responses, you can make
the following assertions:</p>
<ul><li>
<p><code>assert_select_encoded</code> - <a
href="../Assertions.html">Assertions</a> on <a
href="../../HTML.html">HTML</a> encoded inside XML, for example for dealing
with feed item descriptions.</p>
</li><li>
<p><code>assert_select_email</code> - <a
href="../Assertions.html">Assertions</a> on the <a
href="../../HTML.html">HTML</a> body of an e-mail.</p>
</li></ul>

<p>Also see <a href="../../HTML/Selector.html">HTML::Selector</a> to learn how
to use selectors.</p>

    </div>
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>A</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="SelectorAssertions.html#method-i-assert_select">assert_select</a>,
              </li>
            
              
              <li>
                <a href="SelectorAssertions.html#method-i-assert_select_email">assert_select_email</a>,
              </li>
            
              
              <li>
                <a href="SelectorAssertions.html#method-i-assert_select_encoded">assert_select_encoded</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>C</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="SelectorAssertions.html#method-i-css_select">css_select</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>R</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="SelectorAssertions.html#method-i-response_from_page">response_from_page</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    


    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-assert_select">
            
              <b>assert_select</b>(*args, &amp;block)
            
            <a href="SelectorAssertions.html#method-i-assert_select" name="method-i-assert_select" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>An assertion that selects elements and makes one or more equality tests.</p>

<p>If the first argument is an element, selects all matching elements starting
from (and including) that element and all its children in depth-first
order.</p>

<p>If no element if specified, calling <code>assert_select</code> selects from
the response <a href="../../HTML.html">HTML</a> unless
<code>assert_select</code> is called from within an
<code>assert_select</code> block.</p>

<p>When called with a block <code>assert_select</code> passes an array of
selected elements to the block. Calling <code>assert_select</code> from the
block, with no element specified, runs the assertion on the complete set of
elements selected by the enclosing assertion. Alternatively the array may
be iterated through so that <code>assert_select</code> can be called
separately for each element.</p>

<h4 id="method-i-assert_select-label-Example">Example</h4>

<p>If the response contains two ordered lists, each with four list elements
then:</p>

<pre class="ruby"><span class="ruby-identifier">assert_select</span> <span class="ruby-string">&quot;ol&quot;</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">elements</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">elements</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">assert_select</span> <span class="ruby-identifier">element</span>, <span class="ruby-string">&quot;li&quot;</span>, <span class="ruby-value">4</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>will pass, as will:</p>

<pre>assert_select &quot;ol&quot; do
  assert_select &quot;li&quot;, 8
end</pre>

<p>The selector may be a CSS selector expression (<a
href="../../String.html">String</a>), an expression with substitution
values, or an <a href="../../HTML/Selector.html">HTML::Selector</a> object.</p>

<h3 id="method-i-assert_select-label-Equality+Tests">Equality Tests</h3>

<p>The equality test may be one of the following:</p>
<ul><li>
<p><code>true</code> - Assertion is true if at least one element selected.</p>
</li><li>
<p><code>false</code> - Assertion is true if no element selected.</p>
</li><li>
<p><code>String/Regexp</code> - Assertion is true if the text value of at
least one element matches the string or regular expression.</p>
</li><li>
<p><code>Integer</code> - Assertion is true if exactly that number of elements
are selected.</p>
</li><li>
<p><code>Range</code> - Assertion is true if the number of selected elements
fit the range.</p>
</li></ul>

<p>If no equality test specified, the assertion is true if at least one
element selected.</p>

<p>To perform more than one equality tests, use a hash with the following
keys:</p>
<ul><li>
<p><code>:text</code> - Narrow the selection to elements that have this text
value (string or regexp).</p>
</li><li>
<p><code>:html</code> - Narrow the selection to elements that have this <a
href="../../HTML.html">HTML</a> content (string or regexp).</p>
</li><li>
<p><code>:count</code> - Assertion is true if the number of selected elements
is equal to this value.</p>
</li><li>
<p><code>:minimum</code> - Assertion is true if the number of selected
elements is at least this value.</p>
</li><li>
<p><code>:maximum</code> - Assertion is true if the number of selected
elements is at most this value.</p>
</li></ul>

<p>If the method is called with a block, once all equality tests are evaluated
the block is called with an array of all matched elements.</p>

<pre># At least one form element
assert_select &quot;form&quot;

# Form element includes four input fields
assert_select &quot;form input&quot;, 4

# Page title is &quot;Welcome&quot;
assert_select &quot;title&quot;, &quot;Welcome&quot;

# Page title is &quot;Welcome&quot; and there is only one title element
assert_select &quot;title&quot;, {count: 1, text: &quot;Welcome&quot;},
    &quot;Wrong title or more than one title element&quot;

# Page contains no forms
assert_select &quot;form&quot;, false, &quot;This page must contain no forms&quot;

# Test the content and style
assert_select &quot;body div.header ul.menu&quot;

# Use substitution values
assert_select &quot;ol&gt;li#?&quot;, /item-\d+/

# All input fields in the form have a name
assert_select &quot;form input&quot; do
  assert_select &quot;[name=?]&quot;, /.+/  # Not empty
end</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-assert_select_source')" id="l_method-i-assert_select_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/a779ea9d57b00bd3b59f5546603271c2d302247c/actionpack/lib/action_dispatch/testing/assertions/selector.rb#L184" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-assert_select_source" class="dyn-source">
                <pre><span class="ruby-comment"># File actionpack/lib/action_dispatch/testing/assertions/selector.rb, line 184</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">assert_select</span>(*<span class="ruby-identifier">args</span>, &amp;<span class="ruby-identifier">block</span>)
  <span class="ruby-comment"># Start with optional element followed by mandatory selector.</span>
  <span class="ruby-identifier">arg</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">shift</span>
  <span class="ruby-ivar">@selected</span> <span class="ruby-operator">||=</span> <span class="ruby-keyword">nil</span>

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">arg</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">HTML</span><span class="ruby-operator">::</span><span class="ruby-constant">Node</span>)
    <span class="ruby-comment"># First argument is a node (tag or text, but also HTML root),</span>
    <span class="ruby-comment"># so we know what we're selecting from.</span>
    <span class="ruby-identifier">root</span> = <span class="ruby-identifier">arg</span>
    <span class="ruby-identifier">arg</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">shift</span>
  <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">arg</span> <span class="ruby-operator">==</span> <span class="ruby-keyword">nil</span>
    <span class="ruby-comment"># This usually happens when passing a node/element that</span>
    <span class="ruby-comment"># happens to be nil.</span>
    <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;First argument is either selector or element to select, but nil found. Perhaps you called assert_select with an element that does not exist?&quot;</span>
  <span class="ruby-keyword">elsif</span> <span class="ruby-ivar">@selected</span>
    <span class="ruby-identifier">root</span> = <span class="ruby-constant">HTML</span><span class="ruby-operator">::</span><span class="ruby-constant">Node</span>.<span class="ruby-identifier">new</span>(<span class="ruby-keyword">nil</span>)
    <span class="ruby-identifier">root</span>.<span class="ruby-identifier">children</span>.<span class="ruby-identifier">concat</span> <span class="ruby-ivar">@selected</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-comment"># Otherwise just operate on the response document.</span>
    <span class="ruby-identifier">root</span> = <span class="ruby-identifier">response_from_page</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-comment"># First or second argument is the selector: string and we pass</span>
  <span class="ruby-comment"># all remaining arguments. Array and we pass the argument. Also</span>
  <span class="ruby-comment"># accepts selector itself.</span>
  <span class="ruby-keyword">case</span> <span class="ruby-identifier">arg</span>
    <span class="ruby-keyword">when</span> <span class="ruby-constant">String</span>
      <span class="ruby-identifier">selector</span> = <span class="ruby-constant">HTML</span><span class="ruby-operator">::</span><span class="ruby-constant">Selector</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">arg</span>, <span class="ruby-identifier">args</span>)
    <span class="ruby-keyword">when</span> <span class="ruby-constant">Array</span>
      <span class="ruby-identifier">selector</span> = <span class="ruby-constant">HTML</span><span class="ruby-operator">::</span><span class="ruby-constant">Selector</span>.<span class="ruby-identifier">new</span>(*<span class="ruby-identifier">arg</span>)
    <span class="ruby-keyword">when</span> <span class="ruby-constant">HTML</span><span class="ruby-operator">::</span><span class="ruby-constant">Selector</span>
      <span class="ruby-identifier">selector</span> = <span class="ruby-identifier">arg</span>
    <span class="ruby-keyword">else</span> <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Expecting a selector as the first argument&quot;</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-comment"># Next argument is used for equality tests.</span>
  <span class="ruby-identifier">equals</span> = {}
  <span class="ruby-keyword">case</span> <span class="ruby-identifier">arg</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">shift</span>
    <span class="ruby-keyword">when</span> <span class="ruby-constant">Hash</span>
      <span class="ruby-identifier">equals</span> = <span class="ruby-identifier">arg</span>
    <span class="ruby-keyword">when</span> <span class="ruby-constant">String</span>, <span class="ruby-constant">Regexp</span>
      <span class="ruby-identifier">equals</span>[<span class="ruby-value">:text</span>] = <span class="ruby-identifier">arg</span>
    <span class="ruby-keyword">when</span> <span class="ruby-constant">Integer</span>
      <span class="ruby-identifier">equals</span>[<span class="ruby-value">:count</span>] = <span class="ruby-identifier">arg</span>
    <span class="ruby-keyword">when</span> <span class="ruby-constant">Range</span>
      <span class="ruby-identifier">equals</span>[<span class="ruby-value">:minimum</span>] = <span class="ruby-identifier">arg</span>.<span class="ruby-identifier">begin</span>
      <span class="ruby-identifier">equals</span>[<span class="ruby-value">:maximum</span>] = <span class="ruby-identifier">arg</span>.<span class="ruby-identifier">end</span>
    <span class="ruby-keyword">when</span> <span class="ruby-constant">FalseClass</span>
      <span class="ruby-identifier">equals</span>[<span class="ruby-value">:count</span>] = <span class="ruby-number">0</span>
    <span class="ruby-keyword">when</span> <span class="ruby-constant">NilClass</span>, <span class="ruby-constant">TrueClass</span>
      <span class="ruby-identifier">equals</span>[<span class="ruby-value">:minimum</span>] = <span class="ruby-number">1</span>
    <span class="ruby-keyword">else</span> <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;I don't understand what you're trying to match&quot;</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-comment"># By default we're looking for at least one match.</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">equals</span>[<span class="ruby-value">:count</span>]
    <span class="ruby-identifier">equals</span>[<span class="ruby-value">:minimum</span>] = <span class="ruby-identifier">equals</span>[<span class="ruby-value">:maximum</span>] = <span class="ruby-identifier">equals</span>[<span class="ruby-value">:count</span>]
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">equals</span>[<span class="ruby-value">:minimum</span>] = <span class="ruby-number">1</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">equals</span>[<span class="ruby-value">:minimum</span>]
  <span class="ruby-keyword">end</span>

  <span class="ruby-comment"># Last argument is the message we use if the assertion fails.</span>
  <span class="ruby-identifier">message</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">shift</span>
  <span class="ruby-comment">#- message = &quot;No match made with selector #{selector.inspect}&quot; unless message</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">shift</span>
    <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Not expecting that last argument, you either have too many arguments, or they're the wrong type&quot;</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">matches</span> = <span class="ruby-identifier">selector</span>.<span class="ruby-identifier">select</span>(<span class="ruby-identifier">root</span>)
  <span class="ruby-comment"># If text/html, narrow down to those elements that match it.</span>
  <span class="ruby-identifier">content_mismatch</span> = <span class="ruby-keyword">nil</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">match_with</span> = <span class="ruby-identifier">equals</span>[<span class="ruby-value">:text</span>]
    <span class="ruby-identifier">matches</span>.<span class="ruby-identifier">delete_if</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">match</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">text</span> = <span class="ruby-string">&quot;&quot;</span>
      <span class="ruby-identifier">stack</span> = <span class="ruby-identifier">match</span>.<span class="ruby-identifier">children</span>.<span class="ruby-identifier">reverse</span>
      <span class="ruby-keyword">while</span> <span class="ruby-identifier">node</span> = <span class="ruby-identifier">stack</span>.<span class="ruby-identifier">pop</span>
        <span class="ruby-keyword">if</span> <span class="ruby-identifier">node</span>.<span class="ruby-identifier">tag?</span>
          <span class="ruby-identifier">stack</span>.<span class="ruby-identifier">concat</span> <span class="ruby-identifier">node</span>.<span class="ruby-identifier">children</span>.<span class="ruby-identifier">reverse</span>
        <span class="ruby-keyword">else</span>
          <span class="ruby-identifier">content</span> = <span class="ruby-identifier">node</span>.<span class="ruby-identifier">content</span>
          <span class="ruby-identifier">text</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">content</span>
        <span class="ruby-keyword">end</span>
      <span class="ruby-keyword">end</span>
      <span class="ruby-identifier">text</span>.<span class="ruby-identifier">strip!</span> <span class="ruby-keyword">unless</span> <span class="ruby-constant">NO_STRIP</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">match</span>.<span class="ruby-identifier">name</span>)
      <span class="ruby-identifier">text</span>.<span class="ruby-identifier">sub!</span>(<span class="ruby-regexp">/\A\n/</span>, <span class="ruby-string">''</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">match</span>.<span class="ruby-identifier">name</span> <span class="ruby-operator">==</span> <span class="ruby-string">&quot;textarea&quot;</span>
      <span class="ruby-keyword">unless</span> <span class="ruby-identifier">match_with</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Regexp</span>) <span class="ruby-operator">?</span> (<span class="ruby-identifier">text</span> <span class="ruby-operator">=~</span> <span class="ruby-identifier">match_with</span>) <span class="ruby-operator">:</span> (<span class="ruby-identifier">text</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">match_with</span>.<span class="ruby-identifier">to_s</span>)
        <span class="ruby-identifier">content_mismatch</span> <span class="ruby-operator">||=</span> <span class="ruby-identifier">sprintf</span>(<span class="ruby-string">&quot;&lt;%s&gt; expected but was\n&lt;%s&gt;.&quot;</span>, <span class="ruby-identifier">match_with</span>, <span class="ruby-identifier">text</span>)
        <span class="ruby-keyword">true</span>
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">match_with</span> = <span class="ruby-identifier">equals</span>[<span class="ruby-value">:html</span>]
    <span class="ruby-identifier">matches</span>.<span class="ruby-identifier">delete_if</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">match</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">html</span> = <span class="ruby-identifier">match</span>.<span class="ruby-identifier">children</span>.<span class="ruby-identifier">map</span>(&amp;<span class="ruby-value">:to_s</span>).<span class="ruby-identifier">join</span>
      <span class="ruby-identifier">html</span>.<span class="ruby-identifier">strip!</span> <span class="ruby-keyword">unless</span> <span class="ruby-constant">NO_STRIP</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">match</span>.<span class="ruby-identifier">name</span>)
      <span class="ruby-keyword">unless</span> <span class="ruby-identifier">match_with</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Regexp</span>) <span class="ruby-operator">?</span> (<span class="ruby-identifier">html</span> <span class="ruby-operator">=~</span> <span class="ruby-identifier">match_with</span>) <span class="ruby-operator">:</span> (<span class="ruby-identifier">html</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">match_with</span>.<span class="ruby-identifier">to_s</span>)
        <span class="ruby-identifier">content_mismatch</span> <span class="ruby-operator">||=</span> <span class="ruby-identifier">sprintf</span>(<span class="ruby-string">&quot;&lt;%s&gt; expected but was\n&lt;%s&gt;.&quot;</span>, <span class="ruby-identifier">match_with</span>, <span class="ruby-identifier">html</span>)
        <span class="ruby-keyword">true</span>
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-comment"># Expecting foo found bar element only if found zero, not if</span>
  <span class="ruby-comment"># found one but expecting two.</span>
  <span class="ruby-identifier">message</span> <span class="ruby-operator">||=</span> <span class="ruby-identifier">content_mismatch</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">matches</span>.<span class="ruby-identifier">empty?</span>
  <span class="ruby-comment"># Test minimum/maximum occurrence.</span>
  <span class="ruby-identifier">min</span>, <span class="ruby-identifier">max</span>, <span class="ruby-identifier">count</span> = <span class="ruby-identifier">equals</span>[<span class="ruby-value">:minimum</span>], <span class="ruby-identifier">equals</span>[<span class="ruby-value">:maximum</span>], <span class="ruby-identifier">equals</span>[<span class="ruby-value">:count</span>]

  <span class="ruby-comment"># FIXME: minitest provides messaging when we use assert_operator,</span>
  <span class="ruby-comment"># so is this custom message really needed?</span>
  <span class="ruby-identifier">message</span> = <span class="ruby-identifier">message</span> <span class="ruby-operator">||</span> <span class="ruby-node">%Q(Expected #{count_description(min, max, count)} matching &quot;#{selector.to_s}&quot;, found #{matches.size}.)</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">count</span>
    <span class="ruby-identifier">assert_equal</span> <span class="ruby-identifier">matches</span>.<span class="ruby-identifier">size</span>, <span class="ruby-identifier">count</span>, <span class="ruby-identifier">message</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">assert_operator</span> <span class="ruby-identifier">matches</span>.<span class="ruby-identifier">size</span>, <span class="ruby-value">:&gt;=</span>, <span class="ruby-identifier">min</span>, <span class="ruby-identifier">message</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">min</span>
    <span class="ruby-identifier">assert_operator</span> <span class="ruby-identifier">matches</span>.<span class="ruby-identifier">size</span>, <span class="ruby-value">:&lt;=</span>, <span class="ruby-identifier">max</span>, <span class="ruby-identifier">message</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">max</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-comment"># If a block is given call that block. Set @selected to allow</span>
  <span class="ruby-comment"># nested assert_select, which can be nested several levels deep.</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-operator">!</span><span class="ruby-identifier">matches</span>.<span class="ruby-identifier">empty?</span>
    <span class="ruby-keyword">begin</span>
      <span class="ruby-identifier">in_scope</span>, <span class="ruby-ivar">@selected</span> = <span class="ruby-ivar">@selected</span>, <span class="ruby-identifier">matches</span>
      <span class="ruby-keyword">yield</span> <span class="ruby-identifier">matches</span>
    <span class="ruby-keyword">ensure</span>
      <span class="ruby-ivar">@selected</span> = <span class="ruby-identifier">in_scope</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-comment"># Returns all matches elements.</span>
  <span class="ruby-identifier">matches</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-assert_select_email">
            
              <b>assert_select_email</b>(&amp;block)
            
            <a href="SelectorAssertions.html#method-i-assert_select_email" name="method-i-assert_select_email" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Extracts the body of an email and runs nested assertions on it.</p>

<p>You must enable deliveries for this assertion to work, use:</p>

<pre> ActionMailer::Base.perform_deliveries = true

assert_select_email do
  assert_select &quot;h1&quot;, &quot;Email alert&quot;
end

assert_select_email do
  items = assert_select &quot;ol&gt;li&quot;
  items.each do
     # Work with items here...
  end
end</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-assert_select_email_source')" id="l_method-i-assert_select_email_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/a779ea9d57b00bd3b59f5546603271c2d302247c/actionpack/lib/action_dispatch/testing/assertions/selector.rb#L409" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-assert_select_email_source" class="dyn-source">
                <pre><span class="ruby-comment"># File actionpack/lib/action_dispatch/testing/assertions/selector.rb, line 409</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">assert_select_email</span>(&amp;<span class="ruby-identifier">block</span>)
  <span class="ruby-identifier">deliveries</span> = <span class="ruby-constant">ActionMailer</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">deliveries</span>
  <span class="ruby-identifier">assert</span> <span class="ruby-operator">!</span><span class="ruby-identifier">deliveries</span>.<span class="ruby-identifier">empty?</span>, <span class="ruby-string">&quot;No e-mail in delivery list&quot;</span>

  <span class="ruby-identifier">deliveries</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">delivery</span><span class="ruby-operator">|</span>
    (<span class="ruby-identifier">delivery</span>.<span class="ruby-identifier">parts</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-operator">?</span> [<span class="ruby-identifier">delivery</span>] <span class="ruby-operator">:</span> <span class="ruby-identifier">delivery</span>.<span class="ruby-identifier">parts</span>).<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">part</span><span class="ruby-operator">|</span>
      <span class="ruby-keyword">if</span> <span class="ruby-identifier">part</span>[<span class="ruby-string">&quot;Content-Type&quot;</span>].<span class="ruby-identifier">to_s</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp">/^text\/html\W/</span>
        <span class="ruby-identifier">root</span> = <span class="ruby-constant">HTML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">part</span>.<span class="ruby-identifier">body</span>.<span class="ruby-identifier">to_s</span>).<span class="ruby-identifier">root</span>
        <span class="ruby-identifier">assert_select</span> <span class="ruby-identifier">root</span>, <span class="ruby-string">&quot;:root&quot;</span>, &amp;<span class="ruby-identifier">block</span>
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-assert_select_encoded">
            
              <b>assert_select_encoded</b>(element = nil, &amp;block)
            
            <a href="SelectorAssertions.html#method-i-assert_select_encoded" name="method-i-assert_select_encoded" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Extracts the content of an element, treats it as encoded <a
href="../../HTML.html">HTML</a> and runs nested assertion on it.</p>

<p>You typically call this method within another assertion to operate on all
currently selected elements. You can also pass an element or array of
elements.</p>

<p>The content of each element is un-encoded, and wrapped in the root element
<code>encoded</code>. It then calls the block with all un-encoded elements.</p>

<pre># Selects all bold tags from within the title of an Atom feed's entries (perhaps to nab a section name prefix)
assert_select &quot;feed[xmlns='http://www.w3.org/2005/Atom']&quot; do
  # Select each entry item and then the title item
  assert_select &quot;entry&gt;title&quot; do
    # Run assertions on the encoded title elements
    assert_select_encoded do
      assert_select &quot;b&quot;
    end
  end
end

# Selects all paragraph tags from within the description of an RSS feed
assert_select &quot;rss[version=2.0]&quot; do
  # Select description element of each feed item.
  assert_select &quot;channel&gt;item&gt;description&quot; do
    # Run assertions on the encoded elements.
    assert_select_encoded do
      assert_select &quot;p&quot;
    end
  end
end</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-assert_select_encoded_source')" id="l_method-i-assert_select_encoded_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/a779ea9d57b00bd3b59f5546603271c2d302247c/actionpack/lib/action_dispatch/testing/assertions/selector.rb#L361" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-assert_select_encoded_source" class="dyn-source">
                <pre><span class="ruby-comment"># File actionpack/lib/action_dispatch/testing/assertions/selector.rb, line 361</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">assert_select_encoded</span>(<span class="ruby-identifier">element</span> = <span class="ruby-keyword">nil</span>, &amp;<span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">case</span> <span class="ruby-identifier">element</span>
    <span class="ruby-keyword">when</span> <span class="ruby-constant">Array</span>
      <span class="ruby-identifier">elements</span> = <span class="ruby-identifier">element</span>
    <span class="ruby-keyword">when</span> <span class="ruby-constant">HTML</span><span class="ruby-operator">::</span><span class="ruby-constant">Node</span>
      <span class="ruby-identifier">elements</span> = [<span class="ruby-identifier">element</span>]
    <span class="ruby-keyword">when</span> <span class="ruby-keyword">nil</span>
      <span class="ruby-keyword">unless</span> <span class="ruby-identifier">elements</span> = <span class="ruby-ivar">@selected</span>
        <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;First argument is optional, but must be called from a nested assert_select&quot;</span>
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Argument is optional, and may be node or array of nodes&quot;</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">fix_content</span> = <span class="ruby-identifier">lambda</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">node</span><span class="ruby-operator">|</span>
    <span class="ruby-comment"># Gets around a bug in the Rails 1.1 HTML parser.</span>
    <span class="ruby-identifier">node</span>.<span class="ruby-identifier">content</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-regexp">/&lt;!\[CDATA\[(.*)(\]\]&gt;)?/</span>) { <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Utils</span>.<span class="ruby-identifier">escapeHTML</span>(<span class="ruby-node">$1</span>) }
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">selected</span> = <span class="ruby-identifier">elements</span>.<span class="ruby-identifier">map</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">elem</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">text</span> = <span class="ruby-identifier">elem</span>.<span class="ruby-identifier">children</span>.<span class="ruby-identifier">select</span>{ <span class="ruby-operator">|</span><span class="ruby-identifier">c</span><span class="ruby-operator">|</span> <span class="ruby-keyword">not</span> <span class="ruby-identifier">c</span>.<span class="ruby-identifier">tag?</span> }.<span class="ruby-identifier">map</span>{ <span class="ruby-operator">|</span><span class="ruby-identifier">c</span><span class="ruby-operator">|</span> <span class="ruby-identifier">fix_content</span>[<span class="ruby-identifier">c</span>] }.<span class="ruby-identifier">join</span>
    <span class="ruby-identifier">root</span> = <span class="ruby-constant">HTML</span><span class="ruby-operator">::</span><span class="ruby-constant">Document</span>.<span class="ruby-identifier">new</span>(<span class="ruby-constant">CGI</span>.<span class="ruby-identifier">unescapeHTML</span>(<span class="ruby-node">&quot;&lt;encoded&gt;#{text}&lt;/encoded&gt;&quot;</span>)).<span class="ruby-identifier">root</span>
    <span class="ruby-identifier">css_select</span>(<span class="ruby-identifier">root</span>, <span class="ruby-string">&quot;encoded:root&quot;</span>, &amp;<span class="ruby-identifier">block</span>)[<span class="ruby-number">0</span>]
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">begin</span>
    <span class="ruby-identifier">old_selected</span>, <span class="ruby-ivar">@selected</span> = <span class="ruby-ivar">@selected</span>, <span class="ruby-identifier">selected</span>
    <span class="ruby-identifier">assert_select</span> <span class="ruby-string">&quot;:root&quot;</span>, &amp;<span class="ruby-identifier">block</span>
  <span class="ruby-keyword">ensure</span>
    <span class="ruby-ivar">@selected</span> = <span class="ruby-identifier">old_selected</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-css_select">
            
              <b>css_select</b>(*args)
            
            <a href="SelectorAssertions.html#method-i-css_select" name="method-i-css_select" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Select and return all matching elements.</p>

<p>If called with a single argument, uses that argument as a selector to match
all elements of the current page. Returns an empty array if no match is
found.</p>

<p>If called with two arguments, uses the first argument as the base element
and the second argument as the selector. Attempts to match the base element
and any of its children. Returns an empty array if no match is found.</p>

<p>The selector may be a CSS selector expression (<a
href="../../String.html">String</a>), an expression with substitution
values (<a href="../../Array.html">Array</a>) or an <a
href="../../HTML/Selector.html">HTML::Selector</a> object.</p>

<pre class="ruby"><span class="ruby-comment"># Selects all div tags</span>
<span class="ruby-identifier">divs</span> = <span class="ruby-identifier">css_select</span>(<span class="ruby-string">&quot;div&quot;</span>)

<span class="ruby-comment"># Selects all paragraph tags and does something interesting</span>
<span class="ruby-identifier">pars</span> = <span class="ruby-identifier">css_select</span>(<span class="ruby-string">&quot;p&quot;</span>)
<span class="ruby-identifier">pars</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">par</span><span class="ruby-operator">|</span>
  <span class="ruby-comment"># Do something fun with paragraphs here...</span>
<span class="ruby-keyword">end</span>

<span class="ruby-comment"># Selects all list items in unordered lists</span>
<span class="ruby-identifier">items</span> = <span class="ruby-identifier">css_select</span>(<span class="ruby-string">&quot;ul&gt;li&quot;</span>)

<span class="ruby-comment"># Selects all form tags and then all inputs inside the form</span>
<span class="ruby-identifier">forms</span> = <span class="ruby-identifier">css_select</span>(<span class="ruby-string">&quot;form&quot;</span>)
<span class="ruby-identifier">forms</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">form</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">inputs</span> = <span class="ruby-identifier">css_select</span>(<span class="ruby-identifier">form</span>, <span class="ruby-string">&quot;input&quot;</span>)
  <span class="ruby-operator">...</span>
<span class="ruby-keyword">end</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-css_select_source')" id="l_method-i-css_select_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/a779ea9d57b00bd3b59f5546603271c2d302247c/actionpack/lib/action_dispatch/testing/assertions/selector.rb#L60" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-css_select_source" class="dyn-source">
                <pre><span class="ruby-comment"># File actionpack/lib/action_dispatch/testing/assertions/selector.rb, line 60</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">css_select</span>(*<span class="ruby-identifier">args</span>)
  <span class="ruby-comment"># See assert_select to understand what's going on here.</span>
  <span class="ruby-identifier">arg</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">shift</span>

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">arg</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">HTML</span><span class="ruby-operator">::</span><span class="ruby-constant">Node</span>)
    <span class="ruby-identifier">root</span> = <span class="ruby-identifier">arg</span>
    <span class="ruby-identifier">arg</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">shift</span>
  <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">arg</span> <span class="ruby-operator">==</span> <span class="ruby-keyword">nil</span>
    <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;First argument is either selector or element to select, but nil found. Perhaps you called assert_select with an element that does not exist?&quot;</span>
  <span class="ruby-keyword">elsif</span> <span class="ruby-keyword">defined?</span>(<span class="ruby-ivar">@selected</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-ivar">@selected</span>
    <span class="ruby-identifier">matches</span> = []

    <span class="ruby-ivar">@selected</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">selected</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">subset</span> = <span class="ruby-identifier">css_select</span>(<span class="ruby-identifier">selected</span>, <span class="ruby-constant">HTML</span><span class="ruby-operator">::</span><span class="ruby-constant">Selector</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">arg</span>.<span class="ruby-identifier">dup</span>, <span class="ruby-identifier">args</span>.<span class="ruby-identifier">dup</span>))
      <span class="ruby-identifier">subset</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">match</span><span class="ruby-operator">|</span>
        <span class="ruby-identifier">matches</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">match</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">matches</span>.<span class="ruby-identifier">any?</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">m</span><span class="ruby-operator">|</span> <span class="ruby-identifier">m</span>.<span class="ruby-identifier">equal?</span>(<span class="ruby-identifier">match</span>) }
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">end</span>

    <span class="ruby-keyword">return</span> <span class="ruby-identifier">matches</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">root</span> = <span class="ruby-identifier">response_from_page</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">case</span> <span class="ruby-identifier">arg</span>
    <span class="ruby-keyword">when</span> <span class="ruby-constant">String</span>
      <span class="ruby-identifier">selector</span> = <span class="ruby-constant">HTML</span><span class="ruby-operator">::</span><span class="ruby-constant">Selector</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">arg</span>, <span class="ruby-identifier">args</span>)
    <span class="ruby-keyword">when</span> <span class="ruby-constant">Array</span>
      <span class="ruby-identifier">selector</span> = <span class="ruby-constant">HTML</span><span class="ruby-operator">::</span><span class="ruby-constant">Selector</span>.<span class="ruby-identifier">new</span>(*<span class="ruby-identifier">arg</span>)
    <span class="ruby-keyword">when</span> <span class="ruby-constant">HTML</span><span class="ruby-operator">::</span><span class="ruby-constant">Selector</span>
      <span class="ruby-identifier">selector</span> = <span class="ruby-identifier">arg</span>
    <span class="ruby-keyword">else</span> <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;Expecting a selector as the first argument&quot;</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">selector</span>.<span class="ruby-identifier">select</span>(<span class="ruby-identifier">root</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
              
      <div class="sectiontitle">Instance Protected methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-response_from_page">
            
              <b>response_from_page</b>()
            
            <a href="SelectorAssertions.html#method-i-response_from_page" name="method-i-response_from_page" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p><code>assert_select</code> and <code>css_select</code> call this to obtain
the content in the <a href="../../HTML.html">HTML</a> page.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-response_from_page_source')" id="l_method-i-response_from_page_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/a779ea9d57b00bd3b59f5546603271c2d302247c/actionpack/lib/action_dispatch/testing/assertions/selector.rb#L425" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-response_from_page_source" class="dyn-source">
                <pre><span class="ruby-comment"># File actionpack/lib/action_dispatch/testing/assertions/selector.rb, line 425</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">response_from_page</span>
  <span class="ruby-identifier">html_document</span>.<span class="ruby-identifier">root</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    